/*******************************************************************************
 * Copyright (c) 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

/**
  * 将一个select的options移动到另一个select。 
  */
function fnMoveAllOptions(objSource, objDes) {
	for (var i = 0; i < objSource.options.length; i++) {
		var optOption = new Option(objSource.options[i].text, objSource.options[i].value);
		objDes.options[objDes.options.length] = optOption;
	}
	fnRemoveAllOptions(objSource);
}

//remove all select item
function fnRemoveAllOptions(obj) {
	obj.options.length = 0;
}

//标识选项
function selectOption(objSource,key){
	for(var i=0;i<objSource.options.length;i++){
		if(objSource.options[i].value==key){
			objSource.options[i].selected=true;
		}else{
			objSource.options[i].selected=false;
		}
	}
}

function fnAddOption(obj,optionValue,optionText){
	if (!isKeyExist(optionValue, obj)) {
		var optOption = new Option(optionText, optionValue);
		obj.options[obj.options.length] = optOption;
	}
}

//add selected item
function addSelect(objSource, objDest) {
	var sourceLen = objSource.options.length;
	for (var i = 0; i < sourceLen; i++) {
		if (objSource.options[i].selected) {
			var searchKey = objSource.options[i].value;
			if (!isKeyExist(searchKey, objDest)) {
				var optOption = new Option(objSource.options[i].text, objSource.options[i].value);
				objDest.options[objDest.options.length] = optOption;
			}
		}
	}
	removeSelectOptions(objSource);
}
//is key is exist in select
function isKeyExist(searchKey, obj) {
	for (var i = 0; i < obj.options.length; i++) {
		if (searchKey == obj.options[i].value) {
			return true;
		}
	}
	return false;
}
function removeSelectOptions(obj) {
	for (var i = obj.length - 1; i >= 0; i--) {
		if (obj.options[i].selected) {
			obj.options[i] = null;
		}
	}
}
/**
 * 交换两个节点
 */
function swapNode(node1,node2)
{
	var parent = node1.parentNode;//父节点
	var t1 = node1.nextSibling;//两节点的相对位置
	var t2 = node2.nextSibling;
	if(t1) parent.insertBefore(node2,t1);
	else parent.appendChild(node2);
	if(t2) parent.insertBefore(node1,t2);
	else parent.appendChild(node1);
} 

function moveOptionUp(obj, count) {
	var index = obj.selectedIndex;
	if (index == -1) {
		return;
	}
	var step = index - parseInt(count);
	if (step >= 0) {
		swapNode(obj.options[index],obj.options[step]);
	} else {
		swapNode(obj.options[index],obj.options[0]);
	}
}
function moveOptionDown(obj, count) {
	var index = obj.selectedIndex;
	if (index == -1) {
		return;
	}
	var step = index + parseInt(count);
	if (step < obj.length) {
		swapNode(obj.options[index],obj.options[step]);
	} else {
		swapNode(obj.options[index],obj.options[obj.length - 1]);
	}
}

